package com.guli.eduService.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.JwtUtils;
import com.guli.commonUtils.R;
import com.guli.eduService.client.service_order.OrderClient;
import com.guli.eduService.entity.EduCourse;
import com.guli.eduService.entity.vo.ChapterVO;
import com.guli.eduService.entity.vo.CourseDetailsVO;
import com.guli.eduService.entity.vo.CourseQueryVO;
import com.guli.eduService.service.impl.EduChapterServiceImpl;
import com.guli.eduService.service.impl.EduCourseServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zxc
 * @date 2023/04/24 21:27
 **/
@Api(description = "前台课程数据展示")
@RestController
//@CrossOrigin
@RequestMapping("/eduService/courseFront")
public class CourseFrontController {
    @Autowired
    private EduCourseServiceImpl courseService;

    @Autowired
    private EduChapterServiceImpl chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "分页显示条件查询所获取到课程信息")
    @PostMapping(value = "getCourseFrontInfoCondition/{currentPage}/{limit}")
    public R pageList(
            @ApiParam(name = "currentPage", value = "当前页码", required = true)
            @PathVariable Long currentPage,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVO courseQuery){
        // 分页显示数据
        Page<EduCourse> pageParam = new Page<EduCourse>(currentPage, limit);
        Map<String, Object> map = courseService.listCourseFrontInfo(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @ApiOperation(value = "根据课程id，来查询当前课程的详细信息")
    @GetMapping("getCourseFrontDetailsInfo/{courseId}")
    public R getCourseDetailsInfo(
            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId,

            HttpServletRequest request){
        // 1.查询到前端页面所要展示的当前课程的详细信息;
        CourseDetailsVO courseDetails = courseService.queryCourseDetails(courseId);
        // 2.查询到前端页面所要展示的当前课程的章节信息;
        List<ChapterVO> chapterData = chapterService.listChapterData(courseId);
        // 3.远程调用service-order微服务中接口，根据课程id和用户id，查询当前用户是否已经购买该课程
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("memberId : " + memberId);
        Boolean isBuyCourse = orderClient.isBuyCourse(memberId , courseId);
        // 4.将所查询到的数据封装到map集合中;
        Map<String , Object> map = new HashMap<>();
        map.put("courseDetails" , courseDetails);
        map.put("chapterData" , chapterData);
        map.put("isBuyCourse" , isBuyCourse);
        return R.ok().data(map);
    }

}
